<?php
/**
 * 此类是支付回调处理业务逻辑的
 * **/
namespace app\client\lib;
use app\common\lib\Redis;
use think\swoole\facade\Task;
use app\common\lib\GatewayCurl;
class PayHandle {

    //充电的
    public static function charge($data, &$msg, $payType = 0) {
        $orderNo = $data['out_trade_no'];
        $openid = '';
        $tradeNo = '';
        $totalFee = 0;
        
        if($payType == 0){ //微信支付
            $openid = $data['openid'];
            $tradeNo = $data['transaction_id'];
            $totalFee = $data['total_fee'];
        }else if($payType == 1){ //支付宝支付
            $openid = $data['buyer_id'];
            $tradeNo = $data['trade_no'];
        }
        
        
        $orderModel = model('ChargeOrder'); //充电订单
        
        //查出订单
        $order = $orderModel->findOne(['order_no' => $orderNo], ['order_id', 'amount', 'charge_quantity', 'charge_type', 'device_id', 'port_no', 'status']);
        if(!$order){
            $msg = '订单不存在';
            return true;
        }
        
        if($order['status'] != 0){
            $msg = '订单已完成';
            return true;
        }
        
        /* if(intval($order['amount'] * 100) !== $totalFee){
         $msg = '金额不符';
         return true;
         } */
        
        $orderId = $order['order_id'];
        
        $device = model('Device')->findOne(['device_id' => $order['device_id']], ['sim_iccid', 'gateway']);
        
        //更新订单状态
        $rs = $orderModel->modify(['order_id' => $orderId], ['trade_no' => $tradeNo, 'pay_status' => 1]);
        if(!$rs){
            $msg = '更新订单状态失败';
            return false;
        }
        
        $portNo = $order['port_no'];
        $quantity = $order['charge_quantity'];
        $chargeType = $order['charge_type'];
        if($chargeType == 2) {
            $quantity = $quantity * 1000; //计量
        }
        Task::async(function() use($orderId, $quantity, $chargeType, $portNo, $device, &$orderModel){
            
            $curl = new GatewayCurl($device['gateway']);
            $result = $curl->send('startCharge', ['orderId' => $orderId, 'deviceId' => $device['sim_iccid'], 'chargeType' => $chargeType, 'portNo' => $portNo, 'quantity' => $quantity]);
            if($result['success'] == false) { //发送失败,退款
                $rs = $orderModel->modify(['order_id' => $orderId], ['status' => 3]);
                unset($orderModel);
                
                $ch = curl_init('http://127.0.0.1:8888/client/chargeRefund');
                curl_setopt($ch,CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
                curl_setopt($ch, CURLOPT_POST, true); //设置为POST方式
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['id' => $orderId]));//POST数据
                $rs = curl_exec($ch);
                curl_close($ch);
                
            }else{
                $redis = new Redis();
                $redis->setex($device['sim_iccid'] . ':' . $portNo, $orderId, $quantity * 60 + 180);
                $redis->close();
                unset($orderModel);
            }
            return true;
            
        });
        
        return true;
        
    }
    
    
    public static function extendCharge($data, &$msg, $payType = 0) {
        $orderNo = $data['out_trade_no'];
        $openid = '';
        $tradeNo = '';
        $totalFee = 0;
        
        if($payType == 0){ //微信支付
            $openid = $data['openid'];
            $tradeNo = $data['transaction_id'];
            $totalFee = $data['total_fee'];
        }else if($payType == 1){ //支付宝支付
            $openid = $data['buyer_id'];
            $tradeNo = $data['trade_no'];
        }
        
        
        $orderModel = model('ExtendChargeOrder'); //充电订单
        
        //查出增加订单
        $order = $orderModel->findOne(['order_no' => $orderNo], ['order_id', 'amount', 'extend_quantity', 'charge_order_id', 'status']);
        if(!$order){
            $msg = '订单不存在';
            return true;
        }
        
        if($order['status'] != 0){
            $msg = '订单已完成';
            return true;
        }
        
        /* if(intval($order['amount'] * 100) !== $totalFee){
         $msg = '金额不符';
         return true;
         } */
        
        $orderId = $order['order_id'];

        $chargeOrder = model('ChargeOrder')->findOne(['order_id' => $order['charge_order_id']], ['device_id', 'charge_type', 'port_no', 'status']);
        if(empty($chargeOrder)){
            $msg = '充电订单不存在';
            //return true;
        }

        $device = model('Device')->findOne(['device_id' => $chargeOrder['device_id']], ['sim_iccid', 'gateway']);
        
        //更新订单状态
        $rs = $orderModel->modify(['order_id' => $orderId], ['trade_no' => $tradeNo, 'pay_status' => 1]);
        if(!$rs){
            $msg = '更新订单状态失败';
            return false;
        }
        
        $portNo = $chargeOrder['port_no'];
        $chargeType = $chargeOrder['charge_type'];
        $quantity = $order['extend_quantity'];
        
        if($chargeType == 2) { //计量
            $quantity = $quantity * 1000;
        }
        
        Task::async(function() use($orderId, $quantity, $chargeType, $portNo, $device, &$orderModel){
            
            $curl = new GatewayCurl($device['gateway']);
            $result = $curl->send('addCharge', ['orderId' => $orderId, 'deviceId' => $device['sim_iccid'], 'portNo' => $portNo, 'chargeType' => $chargeType, 'quantity' => $quantity]);
            if($result['success'] == false) { //发送失败,退款
                $rs = $orderModel->modify(['order_id' => $orderId], ['status' => 2]);
                unset($orderModel);
                
                $ch = curl_init('http://127.0.0.1:8888/client/extendChargeRefund');
                curl_setopt($ch,CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
                curl_setopt($ch, CURLOPT_POST, true); //设置为POST方式
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['id' => $orderId]));//POST数据
                $rs = curl_exec($ch);
                curl_close($ch);
                
            }else{
                $redis = new Redis();
                $redis->setex('extend:' . $device['sim_iccid'] . ':' . $portNo, $orderId, 180);
                $redis->close();
                unset($orderModel);
            }
            return true;
            
        });

        return true;
    }

}
















